package com.huawei.petSystemdemo.ui.pet;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.GlobalViewModel;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.adapter.CommentAdapter;
import com.huawei.petSystemdemo.adapter.PetAdapter;
import com.huawei.petSystemdemo.bean.Commentpet;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.data.CommentRepository;
import com.huawei.petSystemdemo.nearby.MapViewDemoActivity;
import com.huawei.petSystemdemo.nearby.RoutePlanningDemoActivity;
import com.huawei.petSystemdemo.ui.petvideo.PetVideoAct;
import com.huawei.petSystemdemo.utils.ImgRepository;
import com.huawei.petSystemdemo.utils.MyRequest;
import com.huawei.petSystemdemo.utils.OnBindBottomListener;
import com.synnapps.carouselview.CarouselView;
import com.synnapps.carouselview.ImageListener;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Detail_page_Activity extends AppCompatActivity {


    int id;
    Map<String, Object> responseBody = new HashMap<>();
    private TextView detail_type;
    private TextView detail_price;
    private ImageView female;
    private ImageView mela;
    private TextView detail_name;
    private TextView detail_birth;
    private TextView detail_description;
    private LinearLayout detail_share;
    private LinearLayout detail_collection;
    private LinearLayout detail_self_chat;
    private TextView detail_buy;
    private TextView detail_add;

    private int current = 1;
    private int size = 1;
//
    private ImageView top;
    private String imagename;

    private Button button;

    private Button nearby;
    private Button nearby2;

    Map<String, Object> requestBody = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_page);
//        this.getSupportActionBar().hide();

        detail_type = findViewById(R.id.detail_type);
        detail_price = findViewById(R.id.detailed_price);
        female = findViewById(R.id.detail_female);
        mela = findViewById(R.id.detail_male);
        detail_name = findViewById(R.id.detail_name);
        detail_birth = findViewById(R.id.detail_birth);
        detail_description = findViewById(R.id.detail_description);
//        detail_share = findViewById(R.id.detail_share);
//        detail_collection = findViewById(R.id.detail_collection);
//        detail_self_chat = findViewById(R.id.detail_self_chat);
        detail_buy = findViewById(R.id.detail_buy);
        detail_add = findViewById(R.id.detail_add);

        top=findViewById(R.id.top);


        button = findViewById(R.id.vedio_pet);

        nearby = findViewById(R.id.nearby);
        nearby2 = findViewById(R.id.nearby2);

        Intent intent = getIntent();
        id = intent.getIntExtra("id", -1);
        responseBody.put("id", id);

        MyRequest.get("/pet/detailed",responseBody, json -> {
            JSONObject object = JSON.parseObject(json);
            int status = object.getInteger("statusCode");
            if(status != 200) {
                System.out.println("获取详细信息出错");
                finish();
            } else {
                Pets pets = object.getObject("data", Pets.class);

                detail_type.setText(pets.getType());
                detail_price.setText(pets.getPrice() + "");
                String sex = pets.getSex();
                if(sex.equals("公")) {
                    mela.setVisibility(View.VISIBLE);
                } else if(sex.equals("母")) {
                    female.setVisibility(View.VISIBLE);
                }
                imagename=pets.getImg();
                detail_name.setText(pets.getName());
                SimpleDateFormat format = new SimpleDateFormat("yy:MM:dd hh:mm:ss");
                if(pets.getBirth() != null) {
                    detail_birth.setText(format.format(pets.getBirth()));
                }
                detail_description.setText(pets.getDescription());

                ImgRepository.getImage("/pet/getImg?img=" + pets.getImg(), new Consumer<Bitmap>() {
                    @Override
                    public void accept(Bitmap bitmap) {
                        top.setImageBitmap(bitmap);
                    }
                });
//                ImgRepository.getImage("/shopadress/getImg?img=" + pets.getImg(), new Consumer<Bitmap>() {
//                    @Override
//                    public void accept(Bitmap bitmap) {
//
//                    }
//                });
            }
        });

//        MyRequest.get("/pet/getImg",);




        detail_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( GlobalViewModel.getGlobal().user == null) {
                    Toast.makeText(Detail_page_Activity.this, "先登录吧", Toast.LENGTH_SHORT).show();
                } else {
                    responseBody.clear();
                    responseBody.put("userId", GlobalViewModel.getGlobal().user.getId());
                    responseBody.put("petId", id);

                    MyRequest.post("/shopping/addShopping", responseBody, new Consumer<String>() {
                        @Override
                        public void accept(String s) {
                            System.out.println(s);
                            Toast.makeText(Detail_page_Activity.this, "添加成功，在购物车中等亲来", Toast.LENGTH_SHORT).show();
                        }
                    });

//                    MyRequest.post("/shopping/addShopping", responseBody, new Callback() {
//                        @Override
//                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                            Toast.makeText(Detail_page_Activity.this, "网络出问题了，等会再来吧", Toast.LENGTH_SHORT).show();
//                        }
//
//                        @Override
//                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//                            Toast.makeText(Detail_page_Activity.this, "添加成功，在购物车中等亲来", Toast.LENGTH_SHORT).show();
//                        }
//                    });
                }

            }
        });

        button.setOnClickListener(v->{
            Intent intent_vedio = new Intent(Detail_page_Activity.this, PetVideoAct.class);
            startActivity(intent_vedio);

        });

        nearby.setOnClickListener(v->{
            Intent intent_nearby = new Intent(Detail_page_Activity.this, MapViewDemoActivity.class);
            startActivity(intent_nearby);
        });

        nearby2.setOnClickListener(v->{
            Intent intent_nearby2 = new Intent(Detail_page_Activity.this, RoutePlanningDemoActivity.class);
            startActivity(intent_nearby2);
        });


        RecyclerView list = findViewById(R.id.comment_list);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        list.setLayoutManager(manager);

        List<Commentpet> commentList = new ArrayList<>();

        CommentRepository.getAll(id, new Consumer<List<Commentpet>>() {
            @Override
            public void accept(List<Commentpet> commentpets) {
                commentList.addAll(commentpets);

                CommentAdapter adapter = new CommentAdapter(commentList, Detail_page_Activity.this);
                list.setAdapter(adapter);
            }
        });

    }
}